﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eleanor.BLL;
using System.Dynamic;
using Newtonsoft.Json;

namespace Eleanor.Website.Areas.Admin.Controllers
{
    public class PosternTokenController : Controller
    {
        PosternTokenBLL posternTokenBLL = new PosternTokenBLL();
        //
        // GET: /Admin/PosternToken/

        public ActionResult Index()
        {
            ViewBag.PosternTokens = posternTokenBLL.GetAll();

            ViewBag.TopNavigation = "system";
            ViewBag.SecondNavigation = "PosternToken";
            return View();
        }

        public ActionResult DoCreate()
        {
            int duration;
            if(!int.TryParse(Request.Form["duration"],out duration))
            {
                duration = 30;
            }
            dynamic posternToken = new ExpandoObject();
            posternToken.StartTime = DateTime.Now;
            posternToken.ExpiredTime = posternToken.StartTime.AddMinutes(duration);
            int posternTokenId = posternTokenBLL.Insert(posternToken);
            dynamic result = new ExpandoObject();
            if (posternTokenId > 0)
            {
                result.status = true;
                result.url = "/admin/posterntoken/index";
            }
            else
            {
                result.status = false;
                result.message = "生成失败，请重新生成";
            }
            return Content(JsonConvert.SerializeObject(result));
        }
    }
}
